package com.wusongti.performance.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.wusongti.performance.dao.IPerformanceLogDao;
import com.wusongti.performance.model.entity.PerformanceLogEntity;
import com.wusongti.performance.model.params.PerformanceLogSearchParameter;
import com.wusongti.performance.model.vo.PerformanceLogVO;

import cn.hutool.core.util.StrUtil;

/**
 * @ClassName: PerformanceLogDaoImpl
 * @author: wust
 * @date: 2022/04/28 16:57
 * @Description:
 */
@Repository
public class PerformanceLogDaoImpl implements IPerformanceLogDao {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public int batchSave(List<PerformanceLogEntity> list) {
        int rsSize = 0;

        if (list == null || list.size() == 0) {
            return rsSize;
        }

        final String sql =
            "insert btrace_performance_log(type_name, seq, method_name, line_no, total_time, thread_id, time_millis, create_date)values(?, ?, ?, ?, ?, ?, ?, ?)";

        final List<Object[]> args = new ArrayList<Object[]>(list.size());

        for (PerformanceLogEntity model : list) {
            final Object[] arg = new Object[8];
            arg[0] = model.getTypeName();
            arg[1] = model.getSeq();
            arg[2] = model.getMethodName();
            arg[3] = model.getLineNo();
            arg[4] = model.getTotalTime();
            arg[5] = model.getThreadId();
            arg[6] = model.getTimeMillis();
            arg[7] = model.getCreateDate();

            args.add(arg);
        }

        final int[] rs = jdbcTemplate.batchUpdate(sql, args);

        for (int r : rs) {
            rsSize += r;
        }
        return rsSize;
    }

    @Override
    public List<PerformanceLogVO> findAll(PerformanceLogSearchParameter parameter) {
        final List<Object> paras = new ArrayList<Object>();

        final StringBuffer sql = new StringBuffer(
            "SELECT seq,type_name,method_name,line_no,thread_id,total_time,time_millis,DATE_FORMAT(create_date, '%Y%-%m%-%d') AS create_date FROM btrace_performance_log where total_time > 0");

        if (!StrUtil.isEmptyIfStr(parameter.getTypeName())) {
            sql.append(" and type_name like ? ");
            paras.add("%" + parameter.getTypeName() + "%");
        }

        if (parameter.getThreadId() != null && parameter.getThreadId() > 0) {
            sql.append(" and thread_id = ? ");
            paras.add(parameter.getThreadId());
        }

        if (!StrUtil.isEmptyIfStr(parameter.getMethodName())) {
            sql.append(" and method_name like ? ");
            paras.add("%" + parameter.getMethodName() + "%");
        }

        if (!StrUtil.isEmptyIfStr(parameter.getCreateDate())) {
            sql.append(" and DATE_FORMAT(create_date, '%Y%-%m%-%d') = ? ");
            paras.add(parameter.getCreateDate());
        }

        sql.append(" order by seq asc");

        final List<PerformanceLogVO> list = jdbcTemplate.query(sql.toString(), paras.toArray(),
            new BeanPropertyRowMapper<PerformanceLogVO>(PerformanceLogVO.class));

        return list;
    }

    @Override
    public int deleteAll() {
        jdbcTemplate.execute("delete from btrace_performance_log");
        return 0;
    }
}
